#pragma once
//=============================================================================
// thread.h V20110215 
// (c) 2006-2011, Frans Spijkerman, Avans Hogeschool
//
// Definition of class Thread and AutoThread
//
// Thread:
//    An abstraction of Windows API Threads
//    - implement virtual method run() for thread code
//	  - call method start() to start the thread 
//    - don't call run(), this will be done automatically after start()
//
// AutoThread:
//    Same as Thread, but starts immediately after construction
//=============================================================================

//=============================================================================
class Thread
//=============================================================================
{
public:
	void start()
	{
		// Create thread with handler function, pass this-pointer as parameter
		CreateThread(0, 0, handler, this, 0, 0);
	}
private:
	virtual void run() = 0;

	// A pointer to a member function cannot be passed to CreateThread,
	// so we need an aut-of-class friend that takes the addres of a Thread as 
	// parameter, i.e. the fourth parameter of CreateThread in start().
	friend DWORD WINAPI handler(LPVOID arg)
	{
		((Thread*)arg)->run();
		return 0;
	}
};

//=============================================================================
class AutoThread : protected Thread
//=============================================================================
{
public:
	AutoThread()
	{
		start();
	}
};
